/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: jse2204 - T9
 * <p>Powered by webrx On 2022-09-30 16:45:54
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T10 {

    CountDownLatch latch = new CountDownLatch(1);

    List<String> list = new ArrayList<>();

    void add(){
        String t = Thread.currentThread().getName();
        System.out.printf("%s线程启动..%n",t);
        while(true){
            try{
                TimeUnit.SECONDS.sleep(1);
            }catch(Exception e){
                e.printStackTrace();
            }
            String item = "item:"+list.size();
            list.add(item);
            if(list.size() == 5) latch.countDown();
            System.out.println(item);
            if(list.size()>9) break;
        }
    }

    void check(){
        String t = Thread.currentThread().getName();
        System.out.printf("%s线程启动..%n",t);
        try {
            latch.await(); //wait()中断
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.printf("%s线程:到5个元素了%n",t);
    }

    public static void main(String[] args) {
        T10 t = new T10();

        var a = new Thread(t::add,"工作");
        a.start();

        var b = new Thread(t::check,"统计");
        b.start();


    }
}
